package com.ubxtech.swagger.config;

import com.ubxtech.swagger.properties.SwaggerProperties;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Victor.Xiao
 * @since 2022-01-10 13:44
 */
@Configuration
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerAutoConfiguration{

    @Autowired
    private SwaggerProperties swaggerProperties;

    /**
     * 创建 Docket对象
     * get 对象参数 添加@ParameterObject   或者springdoc.default-flat-param-object=true就能解析对象
     * @return Docket
     */
    @Bean
    public OpenAPI openApi() {
        Info info = new Info()
                .title(swaggerProperties.getTitle())
                .description(swaggerProperties.getDescription())
                .version(swaggerProperties.getVersion())
                .license(new License().name(swaggerProperties.getLicense()).url(swaggerProperties.getLicenseUrl()))
                .contact(new Contact().name(swaggerProperties.getContact().getName())
                        .email(swaggerProperties.getContact().getEmail())
                        .url(swaggerProperties.getContact().getUrl())
                ).termsOfService(swaggerProperties.getTermsOfServiceUrl());
        OpenAPI openApi = new OpenAPI();
        openApi.info(info);
        //API 基础信息
        return  openApi;

    }




    //swagger 3.0
//    private List<RequestParameter> buildRequestParameterFromSwaggerProperties(
//            List<SwaggerProperties.GlobalOperationParameter> globalOperationParameters) {
//        List<RequestParameter> parameters = new ArrayList<>();
//
//        if (Objects.isNull(globalOperationParameters)) {
//            return parameters;
//        }
//        for (SwaggerProperties.GlobalOperationParameter globalOperationParameter : globalOperationParameters) {
//            parameters.add(new RequestParameterBuilder()
//                    .name(globalOperationParameter.getName())
//                    .description(globalOperationParameter.getDescription())
//                    .in(ParameterType.from(globalOperationParameter.getParameterType()))
//                    .required(globalOperationParameter.getRequired())
//                    .hidden(globalOperationParameter.getHidden())
//                    .parameterIndex(globalOperationParameter.getOrder())
//                    .build());
//        }
//        return parameters;
//    }



}
